package com.zjcode.cdcer.service.impl;

import com.chinatechstar.component.commons.utils.RecursiveListUtils;
import com.zjcode.cdcer.enums.CommonEnum;
import com.zjcode.cdcer.mapper.SysDictMapper;
import com.zjcode.cdcer.model.basic.JsonObj;
import com.zjcode.cdcer.model.SysDict;
import com.zjcode.cdcer.service.SysDictService;
import com.zjcode.cdcer.service.TkmEndoscopeDiagnoseNounInfoService;
import java.util.LinkedHashMap;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysDictServiceImpl implements SysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private TkmEndoscopeDiagnoseNounInfoService endoscopeDiagnoseNounInfoService;

    @Override
    public JsonObj findAllDictName() {
        Map<String, Object> map = new HashMap<>();
        //得到所有的字典类型集合
        List<String> dictTypeList = sysDictMapper.getAllDictType(0, CommonEnum.PREFIX.getKey());
        dictTypeList.forEach(dictType -> {
            List<SysDict> list = sysDictMapper.getAllDetailDictByDictType(0, dictType);
            map.put(dictType, list);
        });

        List<LinkedHashMap<String, Object>> allEndoscopeDiagnoseNouns = endoscopeDiagnoseNounInfoService.queryAllEndoscopeDiagnoseNouns();

        // 将allEndoscopeDiagnoseNouns进行分组
        Map<String, List<LinkedHashMap<String, Object>>> endoscopeDiagnoseNounsMap = allEndoscopeDiagnoseNouns
            .stream()
            .collect(Collectors.groupingBy(each -> each
                .get("diagnoseTag")
                .toString()));

        // 下咽
        putDataToMap(endoscopeDiagnoseNounsMap.get("下咽"), "tag1", map);
        // 食管
        putDataToMap(endoscopeDiagnoseNounsMap.get("食管"), "tag2", map);
        // 贲门
        putDataToMap(endoscopeDiagnoseNounsMap.get("贲门"), "tag3", map);
        // 胃
        putDataToMap(endoscopeDiagnoseNounsMap.get("胃"), "tag4", map);
        // 十二指肠
        putDataToMap(endoscopeDiagnoseNounsMap.get("十二指肠"), "tag5", map);

        return JsonObj.success(map);
    }

    private void putDataToMap(List<LinkedHashMap<String, Object>> list,
                              String key,
                              Map<String, Object> map) {
        List<LinkedHashMap<String, Object>> resultList = RecursiveListUtils.queryRecursiveList(list);
        map.put(key, resultList);
    }

    @Override
    public List<SysDict> findAllCerDicts() {
        return sysDictMapper.findAllCerDicts();
    }
}
